package com.jeesite.modules.member.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.general.utils.CosApiUtils;
import com.jeesite.modules.member.entity.MemberApi;
import com.jeesite.modules.member.service.MemberApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.List;

/**
 * @author Mr Wu
 * @date 2025/10/28
 */
@Controller
@RequestMapping(value = "${adminPath}/member")
public class MemberApiController extends BaseController {

    @Autowired
    private MemberApiService memberService;

    /**
     * 会员登录
     */
    @RequestMapping(value = "memberLogin")
    @ResponseBody
    public String memberLogin(String code, String memberId) {
        if (StringUtils.isNotEmpty(memberId)) {
            MemberApi member = new MemberApi();
            member.setMemberId(memberId);
            member.setLastDate(new Date());
            memberService.update(member);
            return renderResult(Global.TRUE, text("登录更新成功！"));
        }
        String openid = memberService.getOpenid(code);
        MemberApi member = new MemberApi();
        member.setOpenId(openid);
        List<MemberApi> list = memberService.findList(member);
        if (list.size()==1) {
            return renderResult(Global.TRUE, text("登录成功！"),list.get(0));
        }else {
            if (list.size()==0) {
                // 新增会员
                System.out.println((IdGen.nextId()).substring(14));
                member.setMemberNick("微信用户"+ (IdGen.nextId()).substring(14));
                member.setMemberAvatar("");
                member.setMemberState("0");
                member.setCreateDate(new Date());
                member.setLastDate(new Date());
                memberService.insert(member);
                return renderResult(Global.TRUE, text("登录成功！"),member);
            }else {
                return renderResult(Global.FALSE, text("登录失败！"));
            }
        }
    }

    /**
     * 会员信息修改
     */
    @RequestMapping(value = "memberUpdate")
    @ResponseBody
    public String memberUpdate(@RequestParam(value = "file") MultipartFile file, MemberApi member) {
        String fileName = file.getOriginalFilename();
        if(!"defaultAvatar.png".equals(fileName)){
            JSONObject files = CosApiUtils.uploadFile(file,"","avatar");
            member.setMemberAvatar(files.getString("filePath"));
            memberService.update(member);
            return renderResult(Global.TRUE, text("信息修改成功！"), files.getString("filePath"));
        }
        memberService.update(member);
        return renderResult(Global.TRUE, text("信息修改成功！"));
    }

}
